package servlets;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.TerminplanerConnectionPool;
import service.TerminplanerService;

@WebServlet("/DeleteTeilnehmerServlet")
public class DeleteTeilnehmerServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection con = null;
    String terminId = null;

    try {
      Long teilnehmerId = Long.valueOf(request.getParameter("TeilnehmerId"));
      terminId = request.getParameter("TerminId");
      con = TerminplanerConnectionPool.getInstanz().getConnection();
      TerminplanerService.getInstanz().deleteTeilnehmer(con,
          TerminplanerService.getInstanz().getTeilnehmerById(con, teilnehmerId));
    } catch (Exception e) {
      request.setAttribute("exception", e.getMessage());
      request.getRequestDispatcher("/WEB-INF/jsps/error.jsp").forward(request, response);
      e.printStackTrace();
      return;
    } finally {
      TerminplanerConnectionPool.getInstanz().saveClose(con);
    }

    response.sendRedirect("TerminServlet?TerminId=" + terminId);
  }
}